package iad.parser;

import iad.dto.IrisDTO;
import iad.neuralnet.attribute.Attribute;
import iad.neuralnet.attribute.iris.IRIS;
import iad.neuralnet.attribute.iris.IrisResult;
import iad.neuralnet.attribute.iris.PetalLength;
import iad.neuralnet.attribute.iris.PetalWidth;
import iad.neuralnet.attribute.iris.SepalLength;
import iad.neuralnet.attribute.iris.SepalWidth;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IrysParser {

	final static Logger logger = LoggerFactory.getLogger(IrysParser.class);
	
	public static IrisDTO parse(File f){

//		List<List<Attribute>>entireList = new ArrayList<List<Attribute>>();
		IrisDTO irisDTO = null;
		
		try
		{
			FileInputStream fstream = new FileInputStream(/*"bezdekIris.data"*/f.getCanonicalPath());
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			
			String strLine;
			String[] tab = null;  
			
			List<Attribute<SepalLength>> sepalLengthColumn = new ArrayList<Attribute<SepalLength>>();
			List<Attribute<SepalWidth>> sepalWidthColumn = new ArrayList<Attribute<SepalWidth>>();
			List<Attribute<PetalLength>> petelLengthColumn = new ArrayList<Attribute<PetalLength>>();
			List<Attribute<PetalWidth>> petalWidthColumn = new ArrayList<Attribute<PetalWidth>>();
			List<Attribute<IrisResult>> irisResultColumn = new ArrayList<Attribute<IrisResult>>();

			//Read File Line By Line
			while ((strLine = br.readLine()) != null && !strLine.equals(""))   
			{
				logger.info(strLine);
				tab=strLine.split(",");
				SepalLength sl = new SepalLength(Double.parseDouble(tab[0]));
				SepalWidth sw = new SepalWidth(Double.parseDouble(tab[1]));
				PetalLength pl = new PetalLength(Double.parseDouble(tab[2]));
				PetalWidth pw = new PetalWidth(Double.parseDouble(tab[3]));
				IrisResult ir = new IrisResult(Enum.valueOf(IRIS.class, 
						IrysParser.adjustResult(tab[4])));
				
//				List<Attribute> row = new ArrayList<Attribute>();
				
//				row.add(new Attribute<SepalLength>(sl));
//				row.add(new Attribute<SepalWidth>(sw));
//				row.add(new Attribute<PetalLength>(pl));
//				row.add(new Attribute<PetalWidth>(pw));
//				row.add(new Attribute<IrisResult>(ir));
				
				sepalLengthColumn.add(new Attribute<SepalLength>(sl));
				sepalWidthColumn.add(new Attribute<SepalWidth>(sw));
				petelLengthColumn.add(new Attribute<PetalLength>(pl));
				petalWidthColumn.add(new Attribute<PetalWidth>(pw));
				irisResultColumn.add(new Attribute<IrisResult>(ir));
//				entireList.add(row);
			}
			
			irisDTO = new IrisDTO(sepalLengthColumn, 
					sepalWidthColumn, petelLengthColumn, petalWidthColumn, irisResultColumn);
			
			//Close the input stream
			in.close();
		}
		catch (IOException ioe){//Catch exception if any
			logger.error("IO Exception: {}", ioe);
		}
		return irisDTO;
	}
	
	public static String adjustResult(String result){
		result = result.replace("-", "_");
		return result.toUpperCase();
	}
	
}
